package com.zx.zxapibackend.controller;

import com.zx.zxapibackend.service.InterfaceChargingService;
import com.zx.zxapicommon.common.BaseResponse;
import com.zx.zxapicommon.exception.BusinessException;
import com.zx.zxapicommon.model.to.GetAvailablePiecesTo;
import com.zx.zxapicommon.model.enums.ErrorCode;
import com.zx.zxapicommon.model.vo.LockChargingVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 张旭
 * @version 1.0
 */
@RestController
@RequestMapping("/charging")
public class InterfaceChargingController {
    @Autowired
    private InterfaceChargingService interfaceChargingService;

    /**
     * 获取接口可购买数量
     * @param getAvailablePiecesTo
     * @return
     */
    @PostMapping("/getPresentAvailablePieces")
    public BaseResponse getPresentAvailablePieces(@RequestBody GetAvailablePiecesTo getAvailablePiecesTo) {
        Long interfaceId = getAvailablePiecesTo.getInterfaceId();
        if (interfaceId < 0L) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return interfaceChargingService.getPresentAvailablePieces(interfaceId);
    }

    /**
     * 更新接口库存
     * @param lockChargingVo
     * @return
     */
    @PostMapping("/updateAvailablePieces")
    public BaseResponse updateAvailablePieces(@RequestBody LockChargingVo lockChargingVo) {
        return interfaceChargingService.updateAvailablePieces(lockChargingVo);
    }

    /**
     * 解锁库存
     *
     * @param lockChargingVo
     * @return
     */
    @PostMapping("/unlockAvailablePieces")
    public BaseResponse unlockAvailablePieces(@RequestBody LockChargingVo lockChargingVo) {
        return interfaceChargingService.unlockAvailablePieces(lockChargingVo);
    }

}
